package com.xjtu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xjtu.dto.billRecord.BillDetailDto;
import com.xjtu.dto.billRecord.BillDto;
import com.xjtu.entity.Bill;
import com.xjtu.entity.BillDetail;
import com.xjtu.mapper.BillDetailMapper;
import com.xjtu.mapper.BillMapper;
import com.xjtu.result.PageResult;
import com.xjtu.result.Result;
import com.xjtu.service.BillService;
import com.xjtu.vo.MedicalVisitVo;
import com.xjtu.vo.billRecord.BillDetailVo;
import com.xjtu.vo.billRecord.BillVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class BillServiceImpl implements BillService {

    @Autowired(required = false)
    BillMapper billMapper;

    @Autowired(required = false)
    BillDetailMapper billDetailMapper;


    @Override
    public void insert(BillDto billDto) {
        log.info("新增参数为{}",billDto);

        Bill bill = new Bill();
        BeanUtils.copyProperties(billDto, bill);
//        bill.setId(1);
        log.info("新增记录为{}",bill);
        billMapper.insert(bill);
        System.out.println(bill.getId());
        if( bill.getId() == 0) {
            throw new RuntimeException("插入失败");
        }

        BillDetail billDetail = new BillDetail();
        billDetail.setBillId(bill.getId());
        if(billDto.getSelectBathLevel() != null) {
            billDetail.setBillItemId(billDto.getSelectBathLevel());
            billDetailMapper.insert(billDetail);
        }
        if(billDto.getSelectCleanClothes() != null) {
            billDetail.setBillItemId(billDto.getSelectCleanClothes());
            billDetailMapper.insert(billDetail);
        }
        if(billDto.getSelectCleanLowerPartBody() != null) {
            billDetail.setBillItemId(billDto.getSelectCleanLowerPartBody());
            billDetailMapper.insert(billDetail);
        }
        if(billDto.getSelectFaceClean() != null) {
            billDetail.setBillItemId(billDto.getSelectFaceClean());
            billDetailMapper.insert(billDetail);
        }
        if(billDto.getSelectFootLevel() != null) {
            billDetail.setBillItemId(billDto.getSelectFootLevel());
            billDetailMapper.insert(billDetail);
        }
        if(billDto.getSelectMouthLevel() != null) {
            billDetail.setBillItemId(billDto.getSelectMouthLevel());
            billDetailMapper.insert(billDetail);
        }
        if(billDto.getSelectNursingLevel() !=null) {
            billDetail.setBillItemId(billDto.getSelectNursingLevel());
            billDetailMapper.insert(billDetail);
        }
        if(billDto.getSelectRoom() != null) {
            billDetail.setBillItemId(billDto.getSelectRoom());
            billDetailMapper.insert(billDetail);
        }
        if(billDto.getSelectToiletLevel() != null) {
            billDetail.setBillItemId(billDto.getSelectToiletLevel());
            billDetailMapper.insert(billDetail);
        }
        if(billDto.getSelectWearingLevel() != null) {
            billDetail.setBillItemId(billDto.getSelectWearingLevel());
            billDetailMapper.insert(billDetail);
        }
        if(billDto.getSelectWheelchairLevel() != null) {
            billDetail.setBillItemId(billDto.getSelectWheelchairLevel());
            billDetailMapper.insert(billDetail);
        }


//        for(BillDetailDto item: billDto.getItems()){
//
//
//            billDetail.setBillId(bill.getId());
//            billDetail.setBillItemId(item.getId());
//            billDetailMapper.insert(billDetail);
//        }

    }

    @Override
    public PageResult queryAll(Integer page, Integer perPage, String elderlyName) {

        PageHelper.startPage(page, perPage);
        List<BillVo> billVos = elderlyName.equals("null") ? billMapper.selectAllElderInfo() : billMapper.selectElderInfoById(elderlyName);

        System.out.println(billVos);

        PageInfo<BillVo> pageInfo = new PageInfo<>(billVos);
        System.out.println("pageInfo = " + pageInfo);

        long total = pageInfo.getTotal();
        List<BillVo> records = pageInfo.getList();
        return new PageResult(total, records);

    }

    @Override
    public Result<List<BillDetailVo>> queryDetailById(Integer billId) {

        List<BillDetailVo> billDetailVos = billMapper.findDetailById(billId);
        return Result.success(billDetailVos);
    }

    @Override
    public Result deleteById(Long billId) {
        int number = billMapper.deleteByPrimaryKey(billId);
        if(number <= 0 ) {
            return Result.error("记录不存在");
        }

        billDetailMapper.deleteByBillId(billId);
        return Result.success();

    }
}
